/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import models.Cliente;
import controller.ClienteDAO;
import controller.PessoaDAO;
import controller.ProjetoDAO;
import javax.swing.table.DefaultTableModel;
import models.Pessoa;
import utils.nucleo.Nucleo;
import utils.observer.Observer;
/**
 *
 * @author Thiago Lippel
 */
public class GridCliente extends javax.swing.JPanel {

    private class ClienteObserver implements Observer {
        @Override
        public void update() {
            loadGrid();
        }
    };
    
    public GridCliente() {
        initComponents();
        
        ClienteDAO.getInstance().getSubject().registerObserver(new ClienteObserver());
        loadGrid();        
    }
    
    protected void loadGrid(){
        
        DefaultTableModel modelo = (DefaultTableModel) jTlistaClientes.getModel();
        
        modelo.setRowCount(0);
        
        jTlistaClientes.setCellSelectionEnabled(false);
        jTlistaClientes.setRowSelectionAllowed(true);
        
        for (Cliente c : ClienteDAO.getClientes()) {
            modelo.addRow(new Object[]{
                c.getId(),
                c.getNomeFantasia(),
                c.getCnpj(),
                c
            });            
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTlistaClientes = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jBExcluirCliente = new javax.swing.JButton();
        jBNovoCliente = new javax.swing.JButton();
        jBAlterarCliente = new javax.swing.JButton();

        jTlistaClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "Código", "Cliente", "CNPJ", "null"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTlistaClientes);
        jTlistaClientes.getColumnModel().getColumn(3).setMinWidth(0);
        jTlistaClientes.getColumnModel().getColumn(3).setPreferredWidth(0);
        jTlistaClientes.getColumnModel().getColumn(3).setMaxWidth(0);

        jBExcluirCliente.setText("Excluir");
        jBExcluirCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBExcluirClienteActionPerformed(evt);
            }
        });

        jBNovoCliente.setText("Novo Cliente");
        jBNovoCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNovoClienteActionPerformed(evt);
            }
        });

        jBAlterarCliente.setText("Alterar");
        jBAlterarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAlterarClienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(153, Short.MAX_VALUE)
                .addComponent(jBAlterarCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBExcluirCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBNovoCliente)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBNovoCliente)
                    .addComponent(jBExcluirCliente)
                    .addComponent(jBAlterarCliente))
                .addGap(0, 13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBNovoClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNovoClienteActionPerformed
        Nucleo.getInstance().getPrincipal().abrirTela(CadCliente.class, false);
    }//GEN-LAST:event_jBNovoClienteActionPerformed

    private void jBExcluirClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBExcluirClienteActionPerformed
        ClienteDAO.excluir((Cliente)jTlistaClientes.getModel().getValueAt(jTlistaClientes.getSelectedRow(), jTlistaClientes.getColumnCount()-1));
    }//GEN-LAST:event_jBExcluirClienteActionPerformed

    private void jBAlterarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAlterarClienteActionPerformed
        Nucleo.getInstance().getPrincipal().abrirTela(new CadCliente((Cliente)jTlistaClientes.getModel().getValueAt(jTlistaClientes.getSelectedRow(), jTlistaClientes.getColumnCount()-1)),false);
    }//GEN-LAST:event_jBAlterarClienteActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAlterarCliente;
    private javax.swing.JButton jBExcluirCliente;
    private javax.swing.JButton jBNovoCliente;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTlistaClientes;
    // End of variables declaration//GEN-END:variables
}
